var Utils = {
    // 设置缓存
  // var users = this.Utils.storage.get('users');
  // if (users) {}
    storage: {
        set (key, value, expiration = 0) {
            var obj = {}
            obj.data = value
            obj.setTime = new Date().getTime()
            if (expiration) {
                obj.expiration = 60 * 60 * (24 * expiration)
            }
            localStorage.setItem(key, JSON.stringify(obj))
        },
        get (key) {
            var [item = JSON.parse(localStorage.getItem(key)), exp = null, nowTime = new Date().getTime()] = []
            if (item) {
                if (item.data && item.data.expires_in) {
                    exp = item.data.expires_in
                } else if (item.expiration) {
                    exp = item.expiration
                }
                if (exp && nowTime - item.setTime > exp * 1000) {
                    localStorage.removeItem(key)
                    return false
                }
            }
            return item
        },
        remove(key) {
            localStorage.removeItem(key)
        }
    },

    // 去除字符串中所有空格
    // !this.Utils.removeAllSpace(this.fundPassword.new)
    removeAllSpace (str) {
        return str.replace(/\s+/g, '')
    },
    /**
     * 过滤字符串
     */
    paseData(str){
        if(str == "" || str == undefined || str == 'undefined' || str == null || str == 'null'){
            return '';
        }else{
            return str;
        }
    },
    // 限制 只输入数字 包括不能输入字符和小数点
    //  @input="bank.card_number = Utils.number(bank.card_number)" v-model="bank.card_number"
    number (str, float = false) {
      str = String(str);
      // float = true 可以输入小数点
      if (float) {
        return str.replace(/[^\d.]/g, '').replace(/^0{1,}/g,'')
      }
      else {
        return str.replace(/[^\d]/g, '').replace(/^0{1,}/g,'')
      }
    },

    // 禁止输入汉字 和 空格
  //  @input="users.username = Utils.noChinese(users.username)"
    noChinese (str) {
      return str.replace(/[\u4E00-\u9FA5]| | [\uFE30-\uFFA0]/g,'').replace(/(^\s+)|(\s+$)/g, '');
    },
   // 去除字符串前后的空格
   // this.Utils.trim(item)
    trim (str) {
        return str.replace(/(^\s+)|(\s+$)/g, '')
    },
    // 去除字符串换行
    removeLineFeed (str) {
        return str.replace(/[\n]/g, '')
    },
    // 去除字符串回车
    removeEnter (str) {
        return str.replace(/[\r]/g, '')
    },
  // if (this.Utils.checkIsChinese(number)) {}
    // 是否包含汉字
    checkIsChinese (val) {
      var reg = new RegExp('[\\u4E00-\\u9FFF]+', 'g')
      if (reg.test(val)) {
        return true
      }
    },
    /*
    * 显示小数点后面几位
    * str 字符串
    * num 显示小数点后几位小数
    */
  // Utils.toFixed(String(user.balance))
    toFixed (str, num = 2) {
        if (String(str).indexOf('.') > -1) {
          var zeo = ''
          for (var i = 0; i < (num - String(str).substr(String(str).lastIndexOf('.') + 1, str.length).length); i++) {
            zeo += '0'
          }
            return String(str).substr(0, String(str).lastIndexOf('.') + num + 1) + zeo
        } else {
            var zeo = ''
            for (var i = 0; i < num; i++) {
                zeo += '0'
            }
            return String(str) + '.' + zeo
        }
    },
    // 将剩余秒数转换成倒计时
  // this.Utils.formatSeconds(this.time)
    formatSeconds (value) {
        var secondTime = parseInt(value);
        var minuteTime = 0;
        var hourTime = 0;
        if (secondTime > 60) {
            minuteTime = parseInt(secondTime / 60);
            secondTime = parseInt(secondTime % 60);
            if (minuteTime > 60) {
                hourTime = parseInt(minuteTime / 60);
                minuteTime = parseInt(minuteTime % 60);
            }
        }
        if (secondTime < 10) {
            secondTime = '0' + parseInt(secondTime);
        }
        if (minuteTime < 10) {
            minuteTime = '0' + parseInt(minuteTime);
        }
        if (hourTime < 10) {
            hourTime = '0' + parseInt(hourTime);
        }
        return hourTime + ':' + minuteTime + ':' + secondTime;
    },
    // obj 传入时间  返回 yyyy-mm-dd
  // Utils.formatTime(item.time_bought)
  formatTime(obj, format = 'YYYY-MM-DD HH:MM:SS') {
    if (!obj) {
      return;
    }
    if (typeof obj === 'string') {
      obj = Number(obj);
    }
    if (String(new Date(obj).getTime()).length < 13) {
      obj = new Date(obj).getTime() * 1000;
    }
    var date = new Date(obj);
    var date2 = date.toLocaleDateString().split('/');
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    if (date2[1] < 10) {
      date2[1] = '0' + date2[1]
    }
    if (date2[2] < 10) {
      date2[2] = '0' + date2[2]
    }
    if (hours < 10) {
      hours = '0' + hours
    }
    if (minutes < 10) {
      minutes = '0' + minutes
    }
    if (seconds < 10) {
      seconds = '0' + seconds
    }
    var dateDay = date2.join('-');
    if (format === 'YYYY-MM-DD') {
      return dateDay;
    } else if (format === 'HH:MM:SS') {
      return hours + ":" + minutes + ':' + seconds
    } else if (format === 'YYYY-MM-DD HH:MM:SS') {
      return dateDay + ' ' + hours + ":" + minutes + ':' + seconds
    }
  },
  // 过滤json
  // this.Utils.filterData(this.searchs)
  filterData(data) {
    for (const k of Object.keys(data)) {
      if (data[k] == '' || data[k] == ' ') {
        delete data[k];
      }
    }
    return data;
  },
  // 处理文本中img请求头
  // this.Utils.setImg(k['content'], 'add');
  setImg(str, type) {
    var imgReg = /<img.*?(?:>|\/>)/gi;
    // eslint-disable-next-line no-useless-escape
    var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
    var arr = str.match(imgReg);
    var st = str;
    if(arr){
      for (var i = 0; i < arr.length; i++) {
        var src = arr[i].match(srcReg);
        if(type === 'remove'){
          var replace_src = src[1].replace(process.env.imgURL,'');
          st = st.replace(src[1], replace_src);
        } else if(type === 'add'){
          st = st.replace(src[1],  process.env.imgURL + src[1]);
        }
      }
    }
    return st;
  },
    
    // 设置全局AJAX请求头
    setupAjaxHeaders: function() {
        $.ajaxSetup({
            beforeSend: function(xhr) {
                // 从localStorage获取token
                const token = localStorage.getItem('token');
                if (token) {
                    // 设置Authorization头
                    xhr.setRequestHeader('Authorization', 'Bearer ' + token);
                }
            }
        });
    },
};